package org

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
)

const MICRO_GFYX_OPPTY_SERVICE = "micro-gfyx-oppty-service"

type orgRepository struct {
}

// NewOrgRepository 构造函数
func NewOrgRepository() OrgRepositoryIface {
	return &orgRepository{}
}

// OrgSave 新增或更新组织
func (r *orgRepository) OrgSave(ctx context.Context, orgObj *base.OrgItem) (int64, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return 0, err
	}
	defer func() {
		_ = conn.Close()
	}()

	req := &pb.OrgSaveRequest{}
	req.Param = &pb.OrgItem{
		ParentId:  orgObj.ParentOrgId,
		OrgName:   orgObj.OrgName,
		Id:        orgObj.OrgId,
		AreaCodes: orgObj.AreaCodes,
	}

	client := pb.NewBaseOrgServiceClient(conn)
	resp, err := client.OrgSave(ctx, req)
	if err != nil {
		return 0, err
	}
	return resp.GetId(), nil
}

// OrgDelete 删除组织
func (r *orgRepository) OrgDelete(ctx context.Context, param *base.OrgDeleteParams) (int64, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return 0, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBaseOrgServiceClient(conn)
	resp, err := client.OrgDelete(ctx, &pb.OrgDeleteRequest{
		OrgId: param.OrgId,
	})
	if err != nil {
		return 0, err
	}

	return resp.GetId(), nil
}

// OrgList 组织列表
func (r *orgRepository) OrgList(ctx context.Context) ([]*base.OrgItem, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBaseOrgServiceClient(conn)
	resp, err := client.OrgList(ctx, &pb.OrgListRequest{})
	if err != nil {
		return nil, err
	}
	orgList := make([]*base.OrgItem, 0)

	for _, item := range resp.List {
		if item.AreaCodes == nil {
			item.AreaCodes = make([]string, 0)
		}

		u := make([]*base.UserResponsible, 0)
		for _, userResponsible := range item.UserResponsible {
			u = append(u, &base.UserResponsible{
				UserId: userResponsible.UserId,
				Name:   userResponsible.Name,
			})
		}
		orgList = append(orgList, &base.OrgItem{
			ParentOrgId:         item.ParentId,
			OrgName:             item.OrgName,
			OrgId:               item.Id,
			OrgFullName:         item.FullName,
			AreaCodes:           item.AreaCodes,
			UserResponsibleList: u,
		})
	}

	return orgList, nil
}

// OrgImport 导入组织数据
func (r *orgRepository) OrgImport(ctx context.Context, orgList []*base.OrgImportItem) ([]*pb.ImportOrgMsg, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseOrgServiceClient(conn)

	req := &pb.ImportOrgRequest{}
	req.OrgList = make([]*pb.ImportOrgItem, 0)
	for _, u := range orgList {

		req.OrgList = append(req.OrgList, &pb.ImportOrgItem{
			OrgFullName: u.OrgFullName,
			AreaNames:   u.AreaNames,
		})

	}
	resp, err := client.ImportOrgs(ctx, req)
	if err != nil {
		return nil, err
	}
	return resp.MsgList, nil
}
